/* Written By : Chang Hyun Park
 * Student ID : 2007002320
 * Subject	  : Data Communication
 * File Name  : Send.java
 */

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.util.Scanner;


public class Send extends Thread
{
	private DatagramSocket clientSocket;
	private DatagramPacket sendPacket;
//	private DatagramPacket ackRecv;
	private byte[] sendbuff;
//	private byte[] ackbuff;
	private InetAddress remoteAddress;
	private int remotePort;
	private Scanner keyboard;
//	private int ackCount;
	
	public Send(DatagramSocket clientSocket, InetAddress remoteAddress, int remotePort)
	{
		this.clientSocket = clientSocket;
		this.remoteAddress = remoteAddress;
		this.remotePort = remotePort;
//		keyboard = new Scanner(System.in);
	}
	
	public void run()
	{
		while(true)
		{
			sendbuff = new byte[1024];
//			ackCount = client.getAckCount();
			sendPacket = new DatagramPacket(sendbuff, sendbuff.length, remoteAddress, remotePort);
			
			try
			{
//				if(System.in.available()!=0)
//					System.out.println("The following lines were not Sent!");
				while(System.in.available()!=0)
			{//Remove all inputted data while Send was blocked!
					String trash=new String(keyboard.nextLine());
//					System.out.println(trash);
//					System.out.flush();
				}
			}
			catch (IOException e)
			{}
			
			keyboard = new Scanner(System.in);//This is to Remove any string that was received while the receive
			String tempBuffer = new String(keyboard.nextLine());
			sendPacket.setData(tempBuffer.getBytes());
			try
			{
				clientSocket.send(sendPacket);
				this.suspend();	//Stops the Sending thread until the Receive thread receives and Resumes this thread!
				
				//recvThread.resume();
//				String temp2 = new String(sendPacket.getData());
				
//				System.out.println("Error Check Verify Packet Sent!"+temp2);
//				ackbuff = new byte[4];
//				ackRecv = new DatagramPacket(ackbuff, ackbuff.length);
//				clientSocket.setSoTimeout(1000);
//				clientSocket.receive(ackRecv);
//				String tempAck = new String(ackRecv.getData());
//				int recvAck = Integer.parseInt(tempAck);
//				System.out.println("Received Ack is "+recvAck);
//				if(recvAck != ackCount)
//					System.out.println("Error Receiving Ack! "+ackCount+" sent but "+recvAck+" received!");
				
			}catch (SocketTimeoutException e)
			{
				System.out.println("Ack wasn't received in timeout!\nResending Packet!");
				Send s1 = new Send(clientSocket, remoteAddress, remotePort);
				s1.run();
				try
				{
					s1.join();
				} catch (InterruptedException e1)
				{
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
/*	public synchronized void receive()
	{
		try
		{
			clientSocket.send(sendPacket);
			String temp2 = new String(sendPacket.getData());
			
			System.out.println("Error Check Verify Packet Sent!"+temp2);
			ackbuff = new byte[4];
			ackRecv = new DatagramPacket(ackbuff, ackbuff.length);
			clientSocket.setSoTimeout(1000);
			clientSocket.receive(ackRecv);
			String ackRecved = new String(ackRecv.getData());
			System.out.println("received Ack!"+ackRecved);
		}
		catch (Exception e)
		{
		}
	}*/
}
